<template>
  <div class="register-container">
    <div class="tr-box">
      <div class="header">
        <h2 class="title">欢迎注册</h2>
        <p class="subtitle">加入我们，开启全新体验</p>
      </div>
      
      <form @submit.prevent="handleRegister" class="register-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <div class="input-wrapper">
            <i class="icon fas fa-user"></i>
            <input type="text" id="username" v-model="registerForm.username" placeholder="请输入2位以上用户名" @blur="validateUsername" />
          </div>
          <div class="error-message" v-if="errors.username">{{ errors.username }}</div>
        </div>

        <div class="form-group">
          <label for="password">密码</label>
          <div class="input-wrapper">
            <i class="icon fas fa-lock"></i>
            <input type="password" id="password" v-model="registerForm.password" placeholder="请输入5位以上密码" @blur="validatePassword" />
          </div>
          <div class="error-message" v-if="errors.password">{{ errors.password }}</div>
        </div>

        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <div class="input-wrapper">
            <i class="icon fas fa-lock"></i>
            <input type="password" id="confirmPassword" v-model="registerForm.confirmPassword" placeholder="请再次输入密码" @blur="validateConfirmPassword" />
          </div>
          <div class="error-message" v-if="errors.confirmPassword">{{ errors.confirmPassword }}</div>
        </div>

        <button type="submit" class="register-btn">
          <span v-if="!loading">立即注册</span>
          <span v-else class="loading-spinner"></span>
        </button>
      </form>

      <div class="footer">
        <div class="agreement">
          注册即表示同意<span class="highlight">《用户协议》</span>和<span class="highlight">《隐私政策》</span>
        </div>
        <div class="login-link">
          已有账号？<router-link to="/login">立即登录</router-link>
        </div>
      </div>
      
      <div class="social-login">
        <div class="divider">
          <span class="divider-line"></span>
          <span class="divider-text">或通过以下方式注册</span>
          <span class="divider-line"></span>
        </div>
        <div class="social-icons">
          <button class="social-btn wechat">
            <i class="fab fa-weixin"></i>
          </button>
          <button class="social-btn weibo">
            <i class="fab fa-weibo"></i>
          </button>
          <button class="social-btn qq">
            <i class="fab fa-qq"></i>
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="Register">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { getRegister } from '../api/index'
import { showToast } from 'vant';

const router = useRouter()
const registerForm = ref({
  username: '',
  password: '',
  confirmPassword: ''
})

const errors = ref({
  username: '',
  password: '',
  confirmPassword: ''
})

const loading = ref(false)

const validateUsername = () => {
  if (!registerForm.value.username) {
    errors.value.username = '用户名不能为空'
  } else if (registerForm.value.username.length < 1) {
    errors.value.username = '用户名长度不能小于2位'
  } else {
    errors.value.username = ''
  }
}

const validatePassword = () => {
  if (!registerForm.value.password) {
    errors.value.password = '密码不能为空'
  } else if (registerForm.value.password.length < 5) {
    errors.value.password = '密码长度不能小于5位'
  } else {
    errors.value.password = ''
  }
}

const validateConfirmPassword = () => {
  if (!registerForm.value.confirmPassword) {
    errors.value.confirmPassword = '请再次输入密码'
  } else if (registerForm.value.confirmPassword !== registerForm.value.password) {
    errors.value.confirmPassword = '两次输入的密码不一致'
  } else {
    errors.value.confirmPassword = ''
  }
}

const validateForm = () => {
  validateUsername()
  validatePassword()
  validateConfirmPassword()

  return !Object.values(errors.value).some(error => error !== '')
}

const handleRegister = async () => {
  if (!validateForm()) return
  loading.value = true
  try {
    let ret = await getRegister(registerForm.value)
    // console.log("ret", ret);
    if (ret !== undefined) {
      showToast("注册成功")
      router.push({ name: 'login' })
    }
  } catch (e) {
    console.error("注册出错:", e);
    alert(`注册失败: ${e.message}`)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped lang='less'>
@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css');

.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.tr-box {
  background: white;
  border-radius: 16px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  padding: 40px;
  width: 100%;
  max-width: 480px;
  transition: all 0.3s ease;
  animation: fadeInUp 0.6s ease;
  
  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.25);
  }
}

.header {
  text-align: center;
  margin-bottom: 30px;
  
  .title {
    color: #2c3e50;
    margin-bottom: 8px;
    font-size: 28px;
    font-weight: 700;
    letter-spacing: 1px;
  }
  
  .subtitle {
    color: #7f8c8d;
    font-size: 14px;
    font-weight: 400;
  }
}

.register-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  position: relative;
  
  label {
    display: block;
    margin-bottom: 8px;
    font-weight: 500;
    color: #34495e;
    font-size: 14px;
  }
  
  .input-wrapper {
    position: relative;
    display: flex;
    align-items: center;
    
    .icon {
      position: absolute;
      left: 15px;
      color: #7f8c8d;
      font-size: 16px;
    }
    
    input {
      width: 100%;
      padding: 12px 15px 12px 40px;
      border: 1px solid #e0e0e0;
      border-radius: 8px;
      font-size: 14px;
      transition: all 0.3s;
      background-color: #f9f9f9;
      
      &:focus {
        border-color: #667eea;
        box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
        outline: none;
        background-color: white;
      }
      
      &::placeholder {
        color: #bbb;
        font-size: 13px;
      }
    }
  }
  
  .error-message {
    color: #ff4757;
    font-size: 12px;
    margin-top: 5px;
    height: 18px;
    padding-left: 5px;
  }
}

.register-btn {
  background: linear-gradient(to right, #667eea, #764ba2);
  color: white;
  border: none;
  padding: 14px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  margin-top: 10px;
  height: 48px;
  position: relative;
  overflow: hidden;
  
  &:hover {
    background: linear-gradient(to right, #5a6fd1, #6a4090);
    box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
  }
  
  &:active {
    transform: scale(0.98);
  }
  
  .loading-spinner {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 3px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    border-top-color: white;
    animation: spin 1s ease-in-out infinite;
  }
}

.footer {
  margin-top: 25px;
  text-align: center;
  
  .agreement {
    color: #7f8c8d;
    font-size: 12px;
    margin-bottom: 15px;
    
    .highlight {
      color: #667eea;
      cursor: pointer;
      
      &:hover {
        text-decoration: underline;
      }
    }
  }
  
  .login-link {
    color: #7f8c8d;
    font-size: 14px;
    
    a {
      color: #667eea;
      text-decoration: none;
      font-weight: 500;
      margin-left: 5px;
      
      &:hover {
        text-decoration: underline;
      }
    }
  }
}

.social-login {
  margin-top: 30px;
  
  .divider {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    
    .divider-line {
      flex: 1;
      height: 1px;
      background: #e0e0e0;
    }
    
    .divider-text {
      padding: 0 15px;
      color: #7f8c8d;
      font-size: 12px;
    }
  }
  
  .social-icons {
    display: flex;
    justify-content: center;
    gap: 15px;
    
    .social-btn {
      width: 44px;
      height: 44px;
      border-radius: 50%;
      border: none;
      cursor: pointer;
      font-size: 18px;
      color: white;
      transition: all 0.3s;
      display: flex;
      align-items: center;
      justify-content: center;
      
      &.wechat {
        background: #07C160;
        
        &:hover {
          background: #06ad56;
          transform: translateY(-3px);
        }
      }
      
      &.weibo {
        background: #E6162D;
        
        &:hover {
          background: #d01429;
          transform: translateY(-3px);
        }
      }
      
      &.qq {
        background: #12B7F5;
        
        &:hover {
          background: #0ea5e0;
          transform: translateY(-3px);
        }
      }
    }
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* 响应式设计 */
@media (max-width: 600px) {
  .tr-box {
    padding: 30px 20px;
    max-width: 90%;
  }
  
  .title {
    font-size: 24px;
  }
  
  .register-btn {
    padding: 12px;
    font-size: 15px;
  }
}
</style>